home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 August: Tool Chest / Apple_Developer_Group_August_1996_Tool_Chest.iso / Sample Code / Snippets / QuickDraw / CopyBits vs. CopyMask / Test.c < prev    next >
Encoding:
Text File  |  1993-02-18  |  10.7 KB  |  452 lines  |  [TEXT/KAHL]

  1. ///--------------------------------------------------------------------------------------
  2. //    Test.c
  3. //
  4. //    By: Tony Myles
  5. //
  6. // Copyright © 1992-93 Apple Computer Inc., All rights reserved.
  7. ///--------------------------------------------------------------------------------------
  8.  
  9.  
  10. #include <QuickDrawUtils.h>
  11. #include <DebugUtils.h>
  12. #include <DialogUtils.h>
  13. #include <GWorldUtils.h>
  14. #include "Sample.h"
  15. #include "Test.h"
  16.  
  17.  
  18. ///--------------------------------------------------------------------------------------
  19. //    LetTheGameBegin
  20. ///--------------------------------------------------------------------------------------
  21.  
  22. void LetTheGameBegin(DialogPtr srcDialogP)
  23. {
  24.     OSErr err;
  25.     CGrafPtr saveCPort;
  26.     GDHandle saveGDevice;
  27.     PixMapHandle srcPixMap;
  28.     GWorldPtr pictGWorld = NULL, maskGWorld = NULL;
  29.     GrafPtr offScrnPort = NULL;
  30.     RgnHandle maskRgn = NULL;
  31.     Rect srcRect, dstRect, itemRect;
  32.     short frame;
  33.     long ticks;
  34.     Str255 itemStr;
  35.     PixPatHandle pixPatH;
  36.  
  37.     SetCursor(*GetCursor(watchCursor));
  38.  
  39.     GetGWorld(&saveCPort, &saveGDevice);
  40.     SetGWorld((CGrafPtr)srcDialogP, NULL);
  41.  
  42.     itemStr[0] = 0;
  43.     SetDItemText(srcDialogP, kCopyBitsFramesItem, itemStr);
  44.     SetDItemText(srcDialogP, kCopyBitsTicksItem, itemStr);
  45.     SetDItemText(srcDialogP, kCopyBitsFPSItem, itemStr);
  46.     SetDItemText(srcDialogP, kCopyMaskFramesItem, itemStr);
  47.     SetDItemText(srcDialogP, kCopyMaskTicksItem, itemStr);
  48.     SetDItemText(srcDialogP, kCopyMaskFPSItem, itemStr);
  49.  
  50.     GetDItemRect(srcDialogP, kCopyBitsPictItem, &itemRect);
  51.     OffsetRect(&itemRect, -itemRect.left, -itemRect.top);
  52.     itemRect.right += kFrameCount;
  53.  
  54.     err = CreateOptimumGWorld(&pictGWorld, &itemRect);
  55.  
  56.     if (err == noErr)
  57.     {
  58.         SetGWorld(pictGWorld, NULL);
  59.         srcPixMap = GetGWorldPixMap(pictGWorld);
  60.         (void)LockPixels(srcPixMap);
  61.  
  62.         pixPatH = GetPixPat(kPixPatResID);
  63.  
  64.         if (pixPatH != NULL)
  65.         {
  66.             FillCRect(&itemRect, pixPatH);
  67.             DisposePixPat(pixPatH);
  68.         }
  69.  
  70.         err = CreateGWorldFromPictResource(&maskGWorld, kApplePictResID);
  71.     }
  72.  
  73.     if (err == noErr)
  74.     {
  75.         offScrnPort = NULL;
  76.         err = CreateGrafPortFromPictResource(kApplePictResID, &offScrnPort);
  77.     }
  78.  
  79.     if (err == noErr)
  80.     {
  81.         maskRgn = NewRgn();
  82.     
  83.         err = BitMapToRegion(maskRgn, &offScrnPort->portBits);
  84.     }
  85.  
  86.     if (err == noErr)
  87.     {
  88.         GetDItemRect(srcDialogP, kCopyBitsPictItem, &itemRect);
  89.         OffsetRgn(maskRgn, itemRect.left, itemRect.top);
  90.     
  91.         srcRect = itemRect;
  92.         OffsetRect(&srcRect, -srcRect.left, -srcRect.top);
  93.         dstRect = itemRect;
  94.     
  95.         SetGWorld((CGrafPtr)srcDialogP, NULL);
  96.  
  97.         ticks = TickCount();
  98.  
  99.         for (frame = 0; frame < kFrameCount; frame++)
  100.         {
  101.             CopyBits((BitMapPtr)*srcPixMap, &srcDialogP->portBits,
  102.                         &srcRect, &dstRect, srcCopy, maskRgn);
  103.     
  104.             srcRect.left++;
  105.             srcRect.right++;
  106.         }
  107.     
  108.         ticks = TickCount() - ticks;
  109.     
  110.         NumToString(kFrameCount, itemStr);
  111.         SetDItemText(srcDialogP, kCopyBitsFramesItem, itemStr);
  112.         NumToString(ticks, itemStr);
  113.         SetDItemText(srcDialogP, kCopyBitsTicksItem, itemStr);
  114.         NumToString((ticks < 60) ? kFrameCount : kFrameCount / (ticks / 60), itemStr);
  115.         SetDItemText(srcDialogP, kCopyBitsFPSItem, itemStr);
  116.     
  117.         GetDItemRect(srcDialogP, kCopyMaskPictItem, &itemRect);
  118.         srcRect = itemRect;
  119.         OffsetRect(&srcRect, -srcRect.left, -srcRect.top);
  120.         dstRect = itemRect;
  121.     
  122.         ticks = TickCount();
  123.     
  124.         for (frame = 0; frame < kFrameCount; frame++)
  125.         {
  126.             CopyMask((BitMapPtr)*srcPixMap, &offScrnPort->portBits, &srcDialogP->portBits,
  127.                         &srcRect, &offScrnPort->portRect, &dstRect);
  128.     
  129.             srcRect.left++;
  130.             srcRect.right++;
  131.         }
  132.     
  133.         ticks = TickCount() - ticks;
  134.     
  135.         NumToString(kFrameCount, itemStr);
  136.         SetDItemText(srcDialogP, kCopyMaskFramesItem, itemStr);
  137.         NumToString(ticks, itemStr);
  138.         SetDItemText(srcDialogP, kCopyMaskTicksItem, itemStr);
  139.         NumToString((ticks < 60) ? kFrameCount : kFrameCount / (ticks / 60), itemStr);
  140.         SetDItemText(srcDialogP, kCopyMaskFPSItem, itemStr);
  141.     
  142.         UnlockPixels(srcPixMap);
  143.     }
  144.  
  145.     if (maskRgn != NULL)
  146.         DisposeRgn(maskRgn);
  147.  
  148.     if (offScrnPort != NULL)
  149.         DisposeGrafPort(offScrnPort);
  150.  
  151.     if (maskGWorld != NULL)
  152.         DisposeGWorld(maskGWorld);
  153.  
  154.     if (pictGWorld != NULL)
  155.         DisposeGWorld(pictGWorld);
  156.  
  157.     SetCursor(&qd.arrow);
  158.  
  159.     SysBeep(0);
  160. }
  161.  
  162.  
  163. ///--------------------------------------------------------------------------------------
  164. // CreateOptimumGWorld
  165. //
  166. //    create a new GWorld optimized for speed in copying
  167. //    to the graphics device that intersects the given rect.
  168. ///--------------------------------------------------------------------------------------
  169.  
  170. OSErr CreateOptimumGWorld(GWorldPtr *optGWorld, Rect *devRect)
  171. {
  172.     OSErr err;
  173.     CGrafPtr saveCPort;
  174.     GDHandle saveGDevice;
  175.     GWorldPtr tempGWorld;
  176.     PixMapHandle pixMapH;
  177.     Rect tempRect = *devRect;
  178.  
  179.     *optGWorld = NULL;
  180.  
  181.     GetGWorld(&saveCPort, &saveGDevice);
  182.  
  183.     LocalToGlobal((Point *)&tempRect.top);
  184.     LocalToGlobal((Point *)&tempRect.bottom);
  185.  
  186.     err = NewGWorld(&tempGWorld, 0, &tempRect, NULL, NULL, 0);
  187.  
  188.     if (err == noErr)
  189.     {
  190.         SetGWorld(tempGWorld, NULL);
  191.         EraseRect(&tempGWorld->portRect);
  192.  
  193.         *optGWorld = tempGWorld;
  194.     }
  195.  
  196.     SetGWorld(saveCPort, saveGDevice);
  197.  
  198.     return err;
  199. }
  200.  
  201.  
  202. ///--------------------------------------------------------------------------------------
  203. //    CreateGWorldFromPict
  204. //
  205. //    creates a offScreen GWorld and draws the specified pict into it
  206. ///--------------------------------------------------------------------------------------
  207.  
  208. OSErr CreateGWorldFromPict(GWorldPtr *pictGWorld, PicHandle pictH)
  209. {
  210.     OSErr err;
  211.     CGrafPtr saveCPort;
  212.     GDHandle saveGDevice;
  213.     GWorldPtr tempGWorld;
  214.     PixMapHandle tempPixMapH;
  215.     Rect pictRect;
  216.  
  217.     *pictGWorld = NULL;
  218.  
  219.     GetGWorld(&saveCPort, &saveGDevice);
  220.  
  221.     pictRect.left = pictRect.top = 0;
  222.     pictRect.right = (**pictH).picFrame.right - (**pictH).picFrame.left;
  223.     pictRect.bottom = (**pictH).picFrame.bottom - (**pictH).picFrame.top;
  224.  
  225.     err = CreateOptimumGWorld(&tempGWorld, &pictRect);
  226.  
  227.     if (err == noErr)
  228.     {
  229.         *pictGWorld = tempGWorld;
  230.  
  231.         SetGWorld(tempGWorld, NULL);
  232.  
  233.         tempPixMapH = GetGWorldPixMap(tempGWorld);
  234.  
  235.         (void)LockPixels(tempPixMapH);
  236.         DrawPicture(pictH, &pictRect);
  237.         UnlockPixels(tempPixMapH);
  238.     }
  239.  
  240.     SetGWorld(saveCPort, saveGDevice);
  241.  
  242.     return err;
  243. }
  244.  
  245.  
  246. ///--------------------------------------------------------------------------------------
  247. // CreateGWorldFromPictResource
  248. ///--------------------------------------------------------------------------------------
  249.  
  250. OSErr CreateGWorldFromPictResource(GWorldPtr *pictGWorldP, short pictResID)
  251. {
  252.     OSErr err;
  253.     PicHandle newPictH;
  254.  
  255.     newPictH = GetPicture(pictResID);
  256.  
  257.     if (newPictH != NULL)
  258.     {
  259.         err = CreateGWorldFromPict(pictGWorldP, newPictH);
  260.  
  261.         ReleaseResource((Handle)newPictH);
  262.     }
  263.  
  264.     return err;
  265. }
  266.  
  267.  
  268. ////--------------------------------------------------------------------------------------
  269. //    CreateGrafPort
  270. //
  271. // originally written by Forrest Tanaka
  272. ////--------------------------------------------------------------------------------------
  273.  
  274. OSErr CreateGrafPort(Rect *newPortRect, GrafPtr *newPort)
  275. {
  276.     OSErr err = noErr;
  277.     GrafPtr savePort;        // save port for later restore
  278.     GrafPtr localPort;    // local copy of GrafPort
  279.     Rect localPortRect;    // local copy of bounds
  280.  
  281.     *newPort = NULL;
  282.  
  283.         // save off the current port
  284.     GetPort(&savePort);
  285.  
  286.         // set the top-left corner of bounds to (0,0)
  287.     localPortRect = *newPortRect;
  288.     OffsetRect(&localPortRect, -newPortRect->left, -newPortRect->top);
  289.  
  290.         // allocate a new GrafPort
  291.     localPort = (GrafPtr)NewPtrClear(sizeof(GrafPort));
  292.  
  293.     if (localPort != NULL)
  294.     {
  295.             // initialize the new port and make the current port
  296.         OpenPort(localPort);
  297.         SetPort(localPort);
  298.         ForeColor(blackColor);
  299.         BackColor(whiteColor);
  300.  
  301.             // initialize and allocate the bitmap
  302.         localPort->portBits.bounds = localPortRect;
  303.           localPort->portBits.rowBytes = ((localPortRect.right + 15) >> 4) << 1;
  304.         localPort->portBits.baseAddr = NewPtrClear(localPort->portBits.rowBytes *
  305.                                                                                             (long)localPortRect.bottom);
  306.  
  307.         if (localPort->portBits.baseAddr != NULL)
  308.         {
  309.                 // clean up the new port
  310.             localPort->portRect = localPortRect;
  311.             ClipRect(&localPortRect);
  312.             RectRgn(localPort->visRgn, &localPortRect);
  313.             EraseRect(&localPortRect);
  314.  
  315.             *newPort = localPort;
  316.         }
  317.         else // allocation failed
  318.         {
  319.                 // capture the error
  320.             err = MemError();
  321.  
  322.                 // deallocate the port
  323.             ClosePort(localPort);
  324.             DisposPtr((Ptr)localPort);
  325.         }
  326.     }
  327.     else
  328.     {
  329.         err = MemError();
  330.     }
  331.  
  332.     SetPort(savePort);
  333.  
  334.     return err;
  335. }
  336.  
  337.  
  338. ////--------------------------------------------------------------------------------------
  339. //    CreateGrafPortFromPictResource
  340. //
  341. //    this routine will set up an offscreen port, and draw a pict into it
  342. //    the offscreen port is the exact size of the pict
  343. ///--------------------------------------------------------------------------------------
  344.  
  345. OSErr CreateGrafPortFromPictResource(short pictID, GrafPtr *offScrnPort)
  346. {
  347.     OSErr            err;
  348.     GrafPtr        savePort;
  349.     PicHandle    pictH;
  350.     Rect            tmpRect;
  351.  
  352.     GetPort(&savePort);
  353.  
  354.     pictH = GetPicture(pictID);
  355.  
  356.     if (pictH != NULL)
  357.     {
  358.         tmpRect.left = tmpRect.top = 0;
  359.         tmpRect.right = (**pictH).picFrame.right - (**pictH).picFrame.left;
  360.         tmpRect.bottom = (**pictH).picFrame.bottom - (**pictH).picFrame.top;
  361.  
  362.             //    create a port
  363.         err = CreateGrafPort(&tmpRect, offScrnPort);
  364.  
  365.         if (err == noErr)
  366.         {
  367.             SetPort(*offScrnPort);
  368.  
  369.                 //    draw the picture
  370.             DrawPicture(pictH, &tmpRect);
  371.         }
  372.  
  373.         ReleaseResource((Handle)pictH);
  374.     }
  375.     else
  376.     {
  377.         err = ResError();
  378.         
  379.         if (err == noErr)
  380.         {
  381.             err = resNotFound;
  382.         }
  383.     }
  384.  
  385.     SetPort(savePort);
  386.  
  387.     return err;
  388. }
  389.  
  390.  
  391. ////--------------------------------------------------------------------------------------
  392. //    DisposeGrafPort
  393. //
  394. // originally written by Forrest Tanaka
  395. ///--------------------------------------------------------------------------------------
  396.  
  397. void DisposeGrafPort(GrafPtr doomedPort)
  398. {
  399.     ClosePort(doomedPort);
  400.     DisposPtr(doomedPort->portBits.baseAddr);
  401.     DisposPtr((Ptr)doomedPort);
  402. }
  403.  
  404.  
  405. ///--------------------------------------------------------------------------------------
  406. // GetDItemText
  407. //
  408. //    get the text of a dialog item
  409. ///--------------------------------------------------------------------------------------
  410.  
  411. void GetDItemText(DialogPtr dlgP, short itemNum, Str255 iStr)
  412. {
  413.     short itemType;
  414.     Rect itemRect;
  415.     Handle itemH;
  416.  
  417.     GetDItem(dlgP, itemNum, &itemType, &itemH, &itemRect);
  418.     GetIText(itemH, iStr);                
  419. }
  420.  
  421.  
  422. ///--------------------------------------------------------------------------------------
  423. // SetDItemText
  424. //
  425. //    set the text of a dialog item
  426. ///--------------------------------------------------------------------------------------
  427.  
  428. void SetDItemText(DialogPtr dlgP, short itemNum, Str255 iStr)
  429. {
  430.     short itemType;
  431.     Rect itemRect;
  432.     Handle itemH;
  433.  
  434.     GetDItem(dlgP, itemNum, &itemType, &itemH, &itemRect);
  435.     SetIText(itemH, iStr);                
  436. }
  437.  
  438.  
  439. ///--------------------------------------------------------------------------------------
  440. // GetDItemRect
  441. //
  442. //    return the rect of a dialog item
  443. ///--------------------------------------------------------------------------------------
  444.  
  445. void GetDItemRect(DialogPtr dlgP, short itemNum, Rect *itemRect)
  446. {
  447.     short itemType;
  448.     Handle itemH;
  449.  
  450.     GetDItem(dlgP, itemNum, &itemType, &itemH, itemRect);
  451. }
  452.